package com.sikaryofficial.backend.domain.mapping;

import com.sikaryofficial.backend.domain.dto.PointsDetailDTO;
import com.sikaryofficial.backend.domain.dto.resp.points.PointsDetailResp;
import com.sikaryofficial.backend.domain.entity.PointsDetail;
import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
import org.mapstruct.Named;
import org.mapstruct.factory.Mappers;

/**
 * @author : qinjinyuan
 * @desc : 积分流水映射
 * @date : 2023/10/31 19:53
 */
@Mapper
public interface PointsDetailMapping {
    PointsDetailMapping INSTANCE = Mappers.getMapper(PointsDetailMapping.class);
    /**
     * 把DTO对象 转为entity
     *
     * @param pointsDetailDTO
     * @return
     */
    PointsDetail coverDtoToEntity(PointsDetailDTO pointsDetailDTO);

    /**
     * 把entity对象 转为DTO
     *
     * @param pointsDetailDTO
     * @return
     */
    @Mapping(
            target = "pointsDelta",
            expression = "java(formatPointsWithSign(pointsDetailDTO))" // 调用自定义方法
    )
    PointsDetailResp coverToResp(PointsDetail pointsDetailDTO);

    // 自定义方法：根据操作类型添加符号
    @Named("formatPointsWithSign")
    default String formatPointsWithSign(PointsDetail pointsDetailDTO) {
        String sign = (pointsDetailDTO.getOpt() == 0) ? "+" : "-";
        return sign + pointsDetailDTO.getPointsDetailValue();
    }
}
